package sanphone.com.runningschool.Saying;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import sanphone.com.runningschool.R;
import sanphone.com.runningschool.entity.Comment;

public class CommentListViewAdapter extends BaseAdapter {
    private List<Comment> comments ;
    private Context mContext;

    public CommentListViewAdapter(Context context,List<Comment> comments) {
        this.comments = comments;
        this.mContext = context;
    }



    @Override
    public int getCount() {
        return comments.size();
    }

    @Override
    public Object getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view == null) {
            final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.item_comment, null);
            holder.commentName = view.findViewById(R.id.comment_name);
            holder.commentText = view.findViewById(R.id.comment_content);
            view.setTag(holder);
        }
        else{
            holder= (ViewHolder) view.getTag();
        }
        final Comment c=comments.get(position);
        holder.commentName.setText(c.getNickName()+"：");
        try {
            holder.commentText.setText(URLDecoder.decode(c.getAnswerText(),"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return view;
    }

    class ViewHolder {
        TextView commentName, commentText;
    }
}
